﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using Drill.Configuration;
using Drill.TestHelpers;
using Drill.TestSupport;
using NUnit.Framework;

namespace Drill.Core.MasterFactories
{
	[TestFixture]
	public class ConfigBasedMasterDependencyResolverFactoryTests
	{
		[Test]
		public void CanConstruct()
		{
			// Arrange
			IDependencyResolverConfiguration config = new DependencyResolverConfiguration();

			// Act
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Assert
			Assert.That(factory, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "config", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructWithNullConfigArgument()
		{
			// Arrange
			IDependencyResolverConfiguration config = null;

			// Act
			// ReSharper disable ExpressionIsAlwaysNull
			// ReSharper disable ObjectCreationAsStatement
			new ConfigBasedMasterDependencyResolverFactory(config);
			// ReSharper restore ObjectCreationAsStatement
			// ReSharper restore ExpressionIsAlwaysNull

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CannotCreateInstanceWithDisabledDependencyResolverConfigArgument()
		{
			// Arrange
			IDependencyResolverConfiguration config = new DependencyResolverConfiguration { Enabled = false };
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolver()
		{
			// Arrange
			IDependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDependencyResolverFactory()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);
			config.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactory()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityDrillBit();
			DrillBitConfiguration drillBitConfig =
				(DrillBitConfiguration)config.DrillBits[0];
			drillBitConfig.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactoryAndRootPathProvider()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityProviderAndRootPathProvider();
			DrillBitConfiguration drillBitConfig =
				(DrillBitConfiguration)config.DrillBits[0];
			drillBitConfig.Type = typeof(TestUnityDependencyResolverFactory).AssemblyQualifiedName;
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanCreateInstanceWithDefaultDependencyResolverAndDrillBitFactoryWithoutRootPathProvidersWithoutSearchPaths()
		{
			// Arrange
			DependencyResolverConfiguration config = TestConfigs.CreateBasicDependencyResolverConfigurationWithUnityProviderWithoutRootPathProvidersWithoutSearchPaths();
			IDependencyResolverFactory factory = new ConfigBasedMasterDependencyResolverFactory(config);

			// Act
			IDependencyResolver dependencyResolver = factory.CreateDependencyResolver();

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}
	}
}